<?php

/**
 * 登录
 * @author Kevin_ren  <330202207@qq.com>
 */

namespace Admin\Controller;

use Common\Controller\BaseManageController;

class LoginController extends BaseManageController
{

    /**
     * 登录页面
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function index()
    {
        $this->display();
    }

    /**
     * 登录
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function loging()
    {
        $verify = I('post.verify');

        if (!check_verify($verify)) {
            $this->simpleError('验证码不正确!', 2000, true);
        }
        $username = I('post.username', '', 'trim');
        $password = I('post.password', '', 'trim,md5');

        $adminObj = D('Admin');
        $admin    = $adminObj->getAdminByUsername($username);

        if (empty($admin) || $admin['password'] != $password) {
            session('verify', null);
            $this->simpleError('用户名或密码不正确!', 2000, true);
        }

        if ($admin['closed'] == 1) {
           session('verify', null);
           $this->simpleError('该账户已经被禁用!', 2000, true);
        }

        $admin['last_time'] = NOW_TIME;
        $admin['last_ip']   = get_client_ip();
        $adminObj->where("id=%d", $admin['id'])->save(array('last_time' => $admin['last_time'], 'last_ip' => $admin['last_ip']));
        session('admin', $admin);
        $this->simpleSuccess('登录成功！', U('Index/index'));
    }

    /**
     * 登出
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function logout()
    {
        session('admin', null);
        $this->success('退出成功', U('Login/index'));
    }

    /**
     * 验证码
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function verify()
    {
        $config = array();
		$config = C('VERIFY');
		$verify = new \Think\Verify($config);
		$verify->entry();
    }
}
